package cn.didi.feidi.location.activity.base;

/**
 * Created by jinxiong on 2017/7/10.
 */


import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;

/***
 * 地图 基础Activity,只包含定位信息，不展示Marker
 */
public abstract class BaseLocationMapActivity extends BaseMapActivity
        implements AMap.InfoWindowAdapter, AMap.OnMarkerClickListener,
        AMapLocationListener {

    private static final String TAG = "BaseLocationMapActivity";


    protected AMapLocationClient mClient;//定位相关
    protected AMapLocationClientOption mOption;//定位配置类
    protected LatLng locationLatLng;//定位到的位置


    protected void initVar() {
        super.initVar();

        mClient = new AMapLocationClient(getApplicationContext());
        mOption = new AMapLocationClientOption();

        mOption.setOnceLocationLatest(true);//只定位一次
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);//高精确模式
        mClient.setLocationOption(mOption);
        mClient.setLocationListener(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (null != mClient) {
            mClient.stopLocation();
            mClient.onDestroy();
            mClient = null;
        }
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (null != aMapLocation) {

            Bundle bundle = aMapLocation.getExtras();

            if (bundle != null) {

                int errorCode = bundle.getInt(MyLocationStyle.ERROR_CODE);
                String errorInfo = bundle.getString(MyLocationStyle.ERROR_INFO);
                int locationType = bundle.getInt(MyLocationStyle.LOCATION_TYPE);

                Log.d(TAG, "onLocationChanged: error" + errorCode + "   errorInfo  " + errorInfo +
                        "  locationType  " + locationType);

                locationLatLng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());

                mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 12));

            } else {
                Log.d(TAG, "onLocationChanged: " + aMapLocation.getErrorInfo());

                Log.d(TAG, "onLocationChanged: bundle null");
            }


        } else {
            Log.d(TAG, "onLocationChanged: 定位失败");
        }

    }

}
